﻿/*
 *   Copyright 2011 Peter Crossley
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *  ----------------------------------------------------------------------------
 *  File:       BasicSettings.cs
 *  Author:     Peter Crossley
 *  ----------------------------------------------------------------------------
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UEC;
namespace UEC.Modules.Basic
{
    public partial class BasicSettings : UserControl
    {
        private BasicService service;
        private CloudManager manager;
        public BasicSettings(BasicService service)
        {
            this.service = service;
            this.manager = service.Manager;
            service.Manager.SaveSettingsEvent += new CloudManager.SaveSettingsHandler(SaveSettings);
            service.Manager.LoadSettingsEvent += new CloudManager.LoadSettingsHandler(LoadSettings);

            InitializeComponent();
            LoadSettings();

            this.ckbHostname.CheckStateChanged += new System.EventHandler(manager.SettingsChanged);
            this.ckbUseCustomHostname.CheckStateChanged += new System.EventHandler(manager.SettingsChanged);
            this.ckbProcessUserData.CheckStateChanged += new System.EventHandler(manager.SettingsChanged);
            this.ckbResetAdminPwd.CheckStateChanged += new System.EventHandler(manager.SettingsChanged);
            this.txtCustomHostname.TextChanged += new System.EventHandler(manager.SettingsChanged);
        }

        protected void LoadSettings(object sender=null, CloudManager.LoadEventArgs e=null)
        {
            if (e != null)
            {
                if (e.Reset)
                {
                    manager.DeleteKeyStore(BasicService.BASIC_SETTINGS);
                }
            }
            ckbHostname.Checked = manager.GetValueAsBoolean(BasicService.BASIC_SETTINGS, "IsForceHostname", Properties.Settings.Default.IsForceHostname);
            ckbUseCustomHostname.Checked = manager.GetValueAsBoolean(BasicService.BASIC_SETTINGS, "IsCustomHostname", Properties.Settings.Default.IsCustomHostname);
            ckbProcessUserData.Checked = manager.GetValueAsBoolean(BasicService.BASIC_SETTINGS, "IsProcessUserDataScripts", Properties.Settings.Default.IsProcessUserDataScripts);
            ckbResetAdminPwd.Checked = manager.GetValueAsBoolean(BasicService.BASIC_SETTINGS, "IsResetAdminPwd", Properties.Settings.Default.IsResetAdminPwd);
            txtCustomHostname.Text = (string)manager.GetValue(BasicService.BASIC_SETTINGS, "CustomHostname", Properties.Settings.Default.CustomHostname);
        }

        protected void SaveSettings(object sender, EventArgs e)
        {
            manager.SetValue(BasicService.BASIC_SETTINGS, "IsForceHostname", (bool)ckbHostname.Checked);
            manager.SetValue(BasicService.BASIC_SETTINGS, "IsCustomHostname", (bool)ckbUseCustomHostname.Checked);
            manager.SetValue(BasicService.BASIC_SETTINGS, "IsProcessUserDataScripts", (bool)ckbProcessUserData.Checked);
            manager.SetValue(BasicService.BASIC_SETTINGS, "IsResetAdminPwd", (bool)ckbResetAdminPwd.Checked);
            manager.SetValue(BasicService.BASIC_SETTINGS, "CustomHostname", txtCustomHostname.Text);
        }

        private void btResetHost_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show(Properties.Resources.ResetAsk, Properties.Resources.SystemMessageTitle, MessageBoxButtons.YesNo);
            if (r == DialogResult.Yes)
            {
                //save and close application
                service.Manager.OnLoadSettings(true);
                service.Manager.OnSaveSettings();
            }
        }
    }
}
